﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace GeckoPhotos.Util
{
    public static class ImageUtil
    {

        /// <summary>
        /// Grabs the MimeType for the ImageFormat specified.  Copied from
        /// http://programcsharp.com/blog/archive/2008/01/17/Get-the-MIME-type-of-a-System.Drawing-Image.aspx
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string GetMimeType(ImageFormat imageFormat)
        {
            foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageDecoders())
            {
                if (codec.FormatID == imageFormat.Guid)
                    return codec.MimeType;
            }

            return "image/unknown";
        }

        /// <summary>
        /// Returns the image as raw bytes in the image format specified.
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] GetImageAsBytes(Image image, ImageFormat imageFormat)
        {
            using (var ms = new System.IO.MemoryStream())
            {
                image.Save(ms, imageFormat);
                byte[] result = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(result, 0, (int)ms.Length);
                return result;
            }
        }

        /// <summary>
        /// Returns a blob of the image passed, resizing to fit the app's thumbnail settings (if necessary).
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte[] CreateResizedImageToFitWidthHeight(int maxWidth, int maxHeight, byte[] bytes, ImageFormat imageFormat)
        {
            using (var bs = new MemoryStream(bytes))
            using (var sourceImage = System.Drawing.Image.FromStream(bs))
            {
                // Determine the factors we'd need to use to shrink down each dimension
                double xFactor = (double)maxWidth / sourceImage.Width;
                double yFactor = (double)maxHeight / sourceImage.Height;
                double factor = Math.Min(xFactor, yFactor);

                // If the image is smaller than the bounding size, then do not resize, but 
                //  still continue with the process to guarantee the ImageFormat of the
                //  returned image
                if (factor > 1)
                    factor = 1;

                int thumbnailWidth = (int)(sourceImage.Width * factor);
                int thumbnailHeight = (int)(sourceImage.Height * factor);

                // Resize the image based on the biggest change from the two dimensions
                using (System.Drawing.Image thumbnailImage = new Bitmap(thumbnailWidth, thumbnailHeight, PixelFormat.Format32bppArgb))
                using (Graphics g = Graphics.FromImage(thumbnailImage))
                {
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    Rectangle rect = new Rectangle(0, 0, thumbnailWidth, thumbnailHeight);
                    g.DrawImage(sourceImage, rect);

                    return GetImageAsBytes(thumbnailImage, imageFormat);
                }
            }
        }


    }
}